﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PCLawPrototype
{
    public partial class ManageTask : ManageRibbonForm
    {
        NewTask newTask = new NewTask();

        public ManageTask()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
        }

        private void ManageTask_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
            ((DStarter)this.MdiParent).SetRPEventVisible(false);
        }
        #region Overrides of ManageRibbonForm

        public override void Add()
        {
            newTask.IsUpdated = false;
            newTask.ShowDialog();

            dgvTask.Rows.Clear();
            if (Common.rows != null)
                for (int index = 0; index < Common.rows.Count - 1; index++)
                {
                    DataGridViewRow row = Common.rows[index];

                    List<string> strs = new List<string>();
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        if (cell.Value != null)
                            strs.Add(cell.Value.ToString());
                        else strs.Add("-nodata-");
                    }
                    string[] list = strs.ToArray();
                    dgvTask.Rows.Add(list);
                }
        }

        public override void Edit()
        {
            newTask.IsUpdated = true;
            newTask.ShowDialog();

            dgvTask.Rows.Clear();
            if (Common.rows != null)
                for (int index = 0; index < Common.rows.Count - 1; index++)
                {
                    DataGridViewRow row = Common.rows[index];

                    List<string> strs = new List<string>();
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        if (cell.Value != null)
                            strs.Add(cell.Value.ToString());
                        else strs.Add("-nodata-");
                    }
                    string[] list = strs.ToArray();
                    dgvTask.Rows.Add(list);
                }
        }

        public override void Delete()
        {
            if (dgvTask.SelectedRows.Count > 0)
            {
                newTask.dgvTask.Rows.RemoveAt(dgvTask.SelectedRows[0].Index);
                dgvTask.Rows.Remove(dgvTask.SelectedRows[0]);
            }
        }

        public override void Reload()
        {
            MessageBox.Show("Reload data from database and fill data to list!", "Description for function", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public override void Active()
        {
        }

        public override void StartSearch()
        {
            MessageBox.Show("Data filtered!");
        }

        #endregion

        private void ManageTask_Activated(object sender, EventArgs e)
        {
            if (this.MdiParent != null)
            {
                ((DStarter) this.MdiParent).SetActiveVisible(false);
                ((DStarter) this.MdiParent).SetRPGTaskVisible(true);
                ((DStarter)this.MdiParent).SetEventFocus();
            }
        }

        private void ManageTask_Deactivate(object sender, EventArgs e)
        {
            if (this.MdiParent != null)
            {
                ((DStarter) this.MdiParent).SetActiveVisible(true);
                ((DStarter) this.MdiParent).SetRPGTaskVisible(false);
            }
        }

        private void dgvTask_Click(object sender, EventArgs e)
        {
            ((DStarter)this.MdiParent).SetEventFocus();
        }

        private void dgvTask_DoubleClick(object sender, EventArgs e)
        {
            newTask.IsUpdated = true;
            newTask.ShowDialog();

            dgvTask.Rows.Clear();
            if (Common.rows != null)
                for (int index = 0; index < Common.rows.Count - 1; index++)
                {
                    DataGridViewRow row = Common.rows[index];

                    List<string> strs = new List<string>();
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        if (cell.Value != null)
                            strs.Add(cell.Value.ToString());
                        else strs.Add("-nodata-");
                    }
                    string[] list = strs.ToArray();
                    dgvTask.Rows.Add(list);
                }
        }
    }
}
